package com.im.partchat.mvp.persenter;

import com.im.partchat.adapter.tree.TreeElement;
import com.im.partchat.base.IBaseView;
import com.im.partchat.bean.Group;
import com.im.partchat.bean.User;
import com.im.partchat.mvp.model.contact.ContactModel;
import com.im.partchat.mvp.model.contact.ContactModelImpl;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by hhz on 2017/7/1.
 */
public class ContactPresenter {

    private IBaseView iBaseView;
    private ContactModel contactModel;

    public ContactPresenter(IBaseView iBaseView) {
        this.iBaseView = iBaseView;
        contactModel = new ContactModelImpl();
    }

    public void getMyOrganization(final String deptId) {
        contactModel.getMyOrganization(deptId)
                .subscribe(new Observer<List<User>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(List<User> value) {
                        iBaseView.onLoadDataSuccess(value, deptId);
                    }

                    @Override
                    public void onError(Throwable e) {
                        iBaseView.showError(e.getMessage());
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getGroup() {
        contactModel.getMyGroup()
                .subscribe(new Observer<List<Group>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<Group> value) {
                        iBaseView.onLoadDataSuccess(value, null);
                    }

                    @Override
                    public void onError(Throwable e) {
                        iBaseView.showError(e.getMessage());
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getFriends() {
        contactModel.getMyFriend().subscribe(new Observer<List<User>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<User> value) {
                iBaseView.onLoadDataSuccess(value, "getFriends");
            }

            @Override
            public void onError(Throwable e) {
                iBaseView.showError(e.getMessage());
                e.printStackTrace();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void getOrganization() {
        contactModel.getOrganization().subscribe(new Observer<List<TreeElement>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<TreeElement> value) {
                iBaseView.onLoadDataSuccess(value, null);
            }

            @Override
            public void onError(Throwable e) {
                iBaseView.showError(e.getMessage());
                e.printStackTrace();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void getUserByKey(String key) {
        contactModel.getUsersByKey(key).subscribe(new Observer<List<User>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<User> value) {
                iBaseView.onLoadDataSuccess(value, "getUserByKey");
            }

            @Override
            public void onError(Throwable e) {
                iBaseView.showError(e.getMessage());
                e.printStackTrace();
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
